<?php  
function get_jadwal($karyawan_id , $periode_id , $date){
	$day = date('d',strtotime($date));
	$query = "SELECT * FROM tmn_jadwal_karyawan 
		WHERE karyawan_id = {$karyawan_id} AND periode_id = {$periode_id} ";
	$res = my_query($query);
	$row = my_fetch_array($res);
	return $row['h'.$day];
}

function get_jadwal_waktu($kode , $hari_id){
	$datas = array('1','2','3','4','5','6','7','8','9');
	if($kode == 'N' ){
		$query = "SELECT jam_masuk , jam_pulang FROM a_jadwal_kerja 
			WHERE waktu_kerja_id = '1' AND hari_id = {$hari_id} AND kelompok = 'N' ";
		$res = my_query($query);
		$row = my_fetch_array($res);
		return $row;
	}elseif( in_array($kode , $datas)){
	$query = "SELECT jam_masuk , jam_pulang FROM a_jadwal_kerja 
		WHERE waktu_kerja_id = '4' AND hari_id = {$hari_id} AND kelompok = '{$kode}' ";
		$res = my_query($query);
		$row = my_fetch_array($res);
		return $row;
	}
	$row = array(
		'jam_masuk'=>'00:00:00',
		'jam_pulang'=>'00:00:00'
	);
	return $row;
}

function get_hari_absen($karyawan_id,$periode_id,$date){
	$index_date = (int) date('d',strtotime($date));
	$query = "SELECT status_absen FROM tmn_hari_realisasi WHERE periode_id = {$periode_id} AND karyawan_id = {$karyawan_id} AND tgl_index = {$index_date} LIMIT 1";
	$res = my_query($query);
	$row = my_fetch_array($res);
	return $row['status_absen'];
}

function cek_status_absen($status_absen){
	$allow = array('1','2','3','4','5','6','7','8','9','N');
	if(in_array( $status_absen, $allow )){
		return true;
	}
	return false;
}


function get_keterangan($karyawan_id , $periode_id){
	$tukar_dinas = "SELECT FROM tukar_karyawan_id FROM a_tukar_shift WHERE karyawan_id = {$karyawan_id} ";
	$result_tukar_dinas = my_query($tukar_dinas );
	if( my_num_rows($result_tukar_dinas) > 0 ){
		$row_tukar_dinas = my_fetch_array($result_tukar_dinas);
		$get_karyawan_id = $row_tukar_dinas['tukar_karyawan_id'];
		$karyawan = loaddata_karyawan($get_karyawan_id);
		return 'Tukar '.$karyawan['nik'];
	}
	
	$tukar_dinas = "SELECT FROM karyawan_id FROM a_tukar_shift WHERE tukar_karyawan_id = {$karyawan_id} ";
	$result_tukar_dinas = my_query($tukar_dinas );
	if(  my_num_rows($result_tukar_dinas) > 0  ){
		$row_tukar_dinas = my_fetch_array($result_tukar_dinas);
		$get_karyawan_id = $row_tukar_dinas['karyawan_id'];
		$karyawan = loaddata_karyawan($get_karyawan_id);
		return 'Tukar '.$karyawan['nik'];
	}
	return '';
}

$known_mime_types=array(
 	"pdf" => "application/pdf",
 	"txt" => "text/plain",
 	"html" => "text/html",
 	"htm" => "text/html",
	"exe" => "application/octet-stream",
	"zip" => "application/zip",
	"doc" => "application/msword",
	"xls" => "application/vnd.ms-excel",
	"ppt" => "application/vnd.ms-powerpoint",
	"gif" => "image/gif",
	"png" => "image/png",
	"jpeg"=> "image/jpg",
	"jpg" =>  "image/jpg",
	"php" => "text/plain"
 );
 
 if($mime_type==''){
	 $file_extension = strtolower(substr(strrchr($file,"."),1));
	 if(array_key_exists($file_extension, $known_mime_types)){
		$mime_type=$known_mime_types[$file_extension];
	 } else {
		$mime_type="application/force-download";
	 };
 };
 
 @ob_end_clean();
header('Content-Type: text/plain' );
 header('Content-Disposition: attachment; filename="slip_absen_'.date('mY').'_'.rand(1000,9999).'.txt"');
 header("Content-Transfer-Encoding: binary");
 header('Accept-Ranges: bytes'); 
 header("Cache-control: private");
 header('Pragma: private');
 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
 //echo '<pre>';
 ?> 
<?php
	$periode_absen_id=get_periode_absen() ;
	if($periode_id==0){
		$periode_id=$periode_absen_id;
	}	
	if( $periode_absen_id==$periode_id ){
		$kalkulasi_periode=0;
	}else{
		$kalkulasi_periode=$periode_id;
	}
	$nik_start = isset($_GET['nik_start']) ? sprintf('%07d',(int)$_GET['nik_start']) :'0000001'; 	
	$nik_end = (isset($_GET['nik_end']) AND (int) $_GET['nik_end'] !='0' )? sprintf('%07d',(int)$_GET['nik_end']) :'9999999'; 	
	$dperiode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulans = array(
		1=>'Januari',
		2=>'Februari',
		3=>'Maret',
		4=>'April',
		5=>'Mei',
		6=>'Juni',
		7=>'Juli',
		8=>'Agustus',
		9=>'September',
		10=>'Oktober',
		11=>'November',
		12=>'Desember',
	);
	$bln = (int)date('m' ,strtotime($dperiode['tanggal']));
	$namabulan = $bulans[$bln]; 	
	$thn = date('Y' ,strtotime($dperiode['tanggal']));
	$query = "
		SELECT a.karyawan_id  FROM karyawan  a
		INNER JOIN tmn_proses_finger b ON a.karyawan_id = b.karyawan_id
		WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
		AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian) 
		AND b.periode_id = {$periode_id}
		AND ( nik BETWEEN {$_GET['nik_start']} AND {$_GET['nik_end']} )
		GROUP BY a.karyawan_id
		ORDER BY a.karyawan_id		
	"; 
 
	$result = my_query($query );
	while($row = my_fetch_array($result )):
		$karyawan = loaddata_karyawan($row['karyawan_id']);	
		$jabatan = my_get_data_by_id('jabatan','jabatan_id',$karyawan['jabatan_id']);
		$golongan_jabatan = my_get_data_by_id('golongan_jabatan','golongan_jabatan_id',(int)$jabatan['golongan_jabatan_id']); 
		$pbi = my_get_data_by_id('pos_biaya','pos_biaya_id',(int)$jabatan['pos_biaya_id']);

?>
 
 
  <?php echo 'SLIP ABSENSI  ( '. $namabulan.' '.$thn.' )'; ?> 			         			                      
  NIK      : <?php echo sprintf('%07d',$karyawan['nik']);?>                      T.M.B.  : <?php echo date('d-m-Y',strtotime($karyawan['tmb'])); ?>
  
  Dept/CCC : <?php echo $jabatan['nomor_posisi']; ?> / <?php echo str_pad($pbi['code'],4);?>              St.Kary : <?php echo ucfirst(str_replace( "Karyawan ","",$karyawan['status_karyawan'])); ?>                     
  Nama     : <?php echo str_pad( $karyawan['nama_gelar'] ,29);?>St.Kel. : <?php echo $karyawan['ptkp_status'].($karyawan['ptkp_tanggung']>0? $karyawan['ptkp_tanggung']: '')?>
  
  Jabatan  : <?php 
  echo (strlen($jabatan['nama_jabatan'])<51) ? $jabatan['nama_jabatan']: substr($jabatan['nama_jabatan'],0,47).'...';
  ?>
  
  _______________________________________________________________  

<?php echo '    '; ?>Tanggal   JDW  Waktu         RLS  Kehadiran     Keterangan
<?php 
$qr = "
SELECT a.proses_date,a.jam_in,a.jam_out ,b.status_absen FROM tmn_proses_finger a
INNER JOIN tmn_hari_realisasi b ON a.periode_id = b.periode_id  AND a.karyawan_id=b.karyawan_id
WHERE a.periode_id = {$periode_id} AND a.karyawan_id = {$row['karyawan_id']}
GROUP BY a.proses_date
ORDER BY a.proses_date ASC";
$rs = my_query($qr);
while($ry = my_fetch_array($rs)){
$hari_id = 1 + date( 'w', strtotime($ry['proses_date']));
$jadwal = get_jadwal($row['karyawan_id'] , $periode_id , $ry['proses_date']);
$status_absen = get_hari_absen($row['karyawan_id'],$periode_id,$ry['proses_date']);
$jtime = get_jadwal_waktu($status_absen , $hari_id);
$keterangan = get_keterangan($row['karyawan_id'] , $periode_id);

$ry['jam_in'] =   cek_status_absen($status_absen)   ?$ry['jam_in'] :'00:00:00'   ;
$ry['jam_out'] =   cek_status_absen($status_absen)   ? $ry['jam_out'] : '00:00:00'   ;
							echo '    ';
							echo str_pad(date('d/m' ,strtotime($ry['proses_date'])),10); //Tanggal 
					        echo str_pad($jadwal,5); //Jadwal
					        echo str_pad(	date('H:i',strtotime($ry['proses_date'].' '.$jtime['jam_masuk'])).' - '.
											date('H:i',strtotime($ry['proses_date'].' '.$jtime['jam_pulang'])),14);  //Waktu
					        echo str_pad($status_absen,5); //Realisasi 
					        echo str_pad(	date('H:i',strtotime($ry['proses_date'].' '.$ry['jam_in'])).' - '.
											date('H:i',strtotime($ry['proses_date'].' '.$ry['jam_out'])),14); //Kehadiran
					        echo  $keterangan ; //Keterangan
						 echo "\n" ;}
						 ?>
 
                         HRIS Krakatau Medika (c) 2011 - <?php echo date('Y'); ?>  
<?php echo chr(12);  
endwhile;?>